//-----------------------------------------------------------------------
// <copyright file="PlacePostDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class PlacePostDAOBase
    {
        #region Common methods
        public virtual PlacePost CreatePlacePostFromReader(IDataReader reader)
        {
            PlacePost item = new PlacePost();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("PlaceId"))) item.PlaceId = (long)reader["PlaceId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatePlacePostFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatePlacePost methods
            
        public virtual void CreatePlacePost(PlacePost placePost)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPlacePostInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, placePost.Title);
                database.AddOutParameter(dbCommand, "@PlaceId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                placePost.PlaceId = (long)database.GetParameterValue(dbCommand, "@PlaceId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatePlacePostException, ex);
            }
        }

        #endregion

        #region UpdatePlacePost methods
        
        public virtual void UpdatePlacePost(PlacePost placePost)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPlacePostUpdate");            
                
                database.AddInParameter(dbCommand, "@PlaceId", DbType.Int64, placePost.PlaceId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, placePost.Title);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatePlacePostException, ex);
            }
        }
        
        #endregion

        #region DeletePlacePost methods
        public virtual void DeletePlacePost(long placeId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPlacePostDelete");
                
                database.AddInParameter(dbCommand, "@PlaceId", DbType.Int64, placeId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletePlacePostException, ex);
            }
        }

        #endregion

        #region GetPlacePost methods
        
        public virtual PlacePost GetPlacePost(long placeId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPlacePostGet");
                
                database.AddInParameter(dbCommand, "@PlaceId", DbType.Int64, placeId);
                
                PlacePost placePost =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        placePost = CreatePlacePostFromReader(reader);
                        reader.Close();
                    }
                }
                return placePost;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetPlacePostException, ex);
            }
        }
        
        #endregion

        #region GetPlacePostList methods
        public virtual PlacePostCollection GetPlacePostList(PlacePostColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPlacePostGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PlacePostCollection placePostCollection = new PlacePostCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        PlacePost placePost = CreatePlacePostFromReader(reader);
                        placePostCollection.Add(placePost);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return placePostCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetPlacePostListException, ex);
            }
        }
        
        public virtual PlacePostCollection GetPlacePostList(PlacePostColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetPlacePostList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetPlacePostList methods
        public virtual PlacePostCollection GetPlacePostListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPlacePostGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PlacePostCollection placePostCollection = new PlacePostCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        PlacePost placePost = CreatePlacePostFromReader(reader);
                        placePostCollection.Add(placePost);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return placePostCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetPlacePostListException, ex);
            }
        }
        
        public virtual PlacePostCollection GetPlacePostListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetPlacePostListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}